package com.ruoyi.project.web.tRoomBill.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.dict.domain.DictData;
import com.ruoyi.project.system.dict.service.IDictDataService;
import com.ruoyi.project.web.tRoomBill.domain.TRoomBill;
import com.ruoyi.project.web.tRoomBill.service.ITRoomBillService;
import com.ruoyi.project.web.tRoominfo.domain.TRoominfo;
import com.ruoyi.project.web.tRoominfo.service.ITRoominfoService;
import com.ruoyi.project.web.tXyBill.domain.TXyBill;
import com.ruoyi.project.web.tXyBill.service.ITXyBillService;

/**
 * 房间流水信息操作处理
 * 
 * @author ruoyi
 * @date 2021-07-06
 */
@Controller
@RequestMapping("/web/tRoomBill")
public class TRoomBillController extends BaseController
{
	private String prefix = "web/tRoomBill";

	@Autowired
	private ITRoomBillService tRoomBillService;

	@Autowired
	private ITXyBillService tXyBillService;

	@Autowired
	private IDictDataService dictDataService;
	
	@Autowired
	private ITRoominfoService tRoominfoService;

	@RequiresPermissions("web:tRoomBill:view")
	@GetMapping()
	public String tRoomBill()
	{
		return prefix + "/tRoomBill";
	}
	
	//统计
	@GetMapping("/tRoomBillTj")
	public String tRoomBillTj()
	{
		return prefix + "/tRoomBillTj";
	}

	/**
	 * 查询房间流水列表
	 */
	@RequiresPermissions("web:tRoomBill:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(TRoomBill tRoomBill)
	{
		startPage();
		List<TRoomBill> list = tRoomBillService.selectTRoomBillList(tRoomBill);
		return getDataTable(list);
	}
	
	/**
	 * 查询房间流水列表
	 */
	@RequiresPermissions("web:tRoomBill:list")
	@PostMapping("/list2")
	@ResponseBody
	public TableDataInfo list2(TRoomBill tRoomBill)
	{
		try {
			startPage();
			
			if(StringUtils.isNotEmpty(tRoomBill.getKssj())){
				tRoomBill.setKssj(tRoomBill.getKssj()+" 08:00:00");
				
				if(StringUtils.isEmpty(tRoomBill.getJssj())){
					DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Date date = fmt.parse(tRoomBill.getKssj());
					
					Calendar   calendar = new GregorianCalendar(); 
					calendar.setTime(date); 
					calendar.add(calendar.DATE,1); 
					date=calendar.getTime(); 
					tRoomBill.setJssj(fmt.format(date));
				}else if(StringUtils.isNotEmpty(tRoomBill.getJssj())){
					DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Date date = fmt.parse(tRoomBill.getJssj()+" 08:00:00");
					
					Calendar   calendar = new GregorianCalendar(); 
					calendar.setTime(date); 
					calendar.add(calendar.DATE,1); 
					date=calendar.getTime(); 
					tRoomBill.setJssj(fmt.format(date));
				}
				
			}
			
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<TRoomBill> list2 = new ArrayList<TRoomBill>();
		List<TRoomBill> list = tRoomBillService.selectTRoomBillList(tRoomBill);
		
		
		Double jcfyHj = 0D;
		Double ylfyHj = 0D;
		Double xyfyHj = 0D;
		Double csfyHj = 0D;
		Double hj = 0D;
		
		for(int i = 0;i<list.size();i++){
			
			jcfyHj += Double.valueOf(list.get(i).getJcfy())  ;
			ylfyHj += Double.valueOf(list.get(i).getYlfy())  ;
			xyfyHj += Double.valueOf(list.get(i).getXyfy())  ;
			csfyHj += Double.valueOf(list.get(i).getCsfy())  ;
			hj += Double.valueOf(list.get(i).getZfy())  ;
			
		}
		TRoomBill tt = new TRoomBill();
		tt.setKssj(tRoomBill.getKssj());
		tt.setJssj(tRoomBill.getJssj());
		tt.setJcfy(jcfyHj+"");
		tt.setYlfy(ylfyHj+"");
		tt.setXyfy(xyfyHj+"");
		tt.setCsfy(csfyHj+"");
		tt.setZfy(hj+"");
		list2.add(tt);
		
		
		return getDataTable(list2);
	}


	/**
	 * 导出房间流水列表
	 */
	@RequiresPermissions("web:tRoomBill:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(TRoomBill tRoomBill)
	{
		List<TRoomBill> list = tRoomBillService.selectTRoomBillList(tRoomBill);
		ExcelUtil<TRoomBill> util = new ExcelUtil<TRoomBill>(TRoomBill.class);
		return util.exportExcel(list, "tRoomBill");
	}

	/**
	 * 新增房间流水
	 */
	@GetMapping("/add")
	public String add()
	{
		return prefix + "/add";
	}

	/**
	 * 新增保存房间流水
	 */
	@RequiresPermissions("web:tRoomBill:add")
	@Log(title = "房间流水", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(TRoomBill tRoomBill)
	{		
		return toAjax(tRoomBillService.insertTRoomBill(tRoomBill));
	}

	/**
	 * 修改房间流水
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		TRoomBill tRoomBill = tRoomBillService.selectTRoomBillById(id);
		mmap.put("tRoomBill", tRoomBill);
		return prefix + "/edit";
	}


	/**
	 * 修改房间流水
	 */
	@GetMapping("/edit2")
	public String edit2(TRoomBill tRoomBill2, ModelMap mmap)
	{
		tRoomBill2.setZt("在线");
		List<TRoomBill> tRoomBillList = tRoomBillService.selectTRoomBillList(tRoomBill2);

		//查询香烟字典表
		DictData xy = new DictData();
		xy.setDictType("xiangyan");
		xy.setStatus("0");
		List<DictData> xyDictList = dictDataService.selectDictDataList(xy);
		
		//查询饮料字典表
		DictData yl = new DictData();
		yl.setDictType("yinliao");
		yl.setStatus("0");
		List<DictData> ylDictList = dictDataService.selectDictDataList(yl);

		//查询房间香烟 饮料数据
		
		if(tRoomBillList.size()>0){
			TXyBill txyBill = new TXyBill();
			txyBill.setRoomBillId(tRoomBillList.get(0).getId());
			List<TXyBill> tXyBillList= tXyBillService.selectTXyBillList(txyBill);
			for(int i = 0;i<xyDictList.size();i++){
				for(int j=0;j<tXyBillList.size();j++){
					DictData xyDictData = xyDictList.get(i);
					TXyBill tXyBill2 = tXyBillList.get(j);
					
					String code = xyDictData.getDictCode().toString();
					if(code.equals(tXyBill2.getDictCode())){
						xyDictList.get(i).setSl(tXyBill2.getSl());
					}
				}
			}
			
			for(int i = 0;i<ylDictList.size();i++){
				for(int j=0;j<tXyBillList.size();j++){
					DictData ylDictData = ylDictList.get(i);
					TXyBill tXyBill2 = tXyBillList.get(j);
					
					String code = ylDictData.getDictCode().toString();
					if(code.equals(tXyBill2.getDictCode())){
						ylDictList.get(i).setSl(tXyBill2.getSl());
					}
				}
			}
			
			mmap.put("xyDictList", xyDictList);
			mmap.put("ylDictList", ylDictList);


			mmap.put("tRoomBill", tRoomBillList.get(0));
		}

		//		TRoomBill tRoomBill = tRoomBillService.selectTRoomBillById(id);
		return prefix + "/edit2";
	}

	/**
	 * 修改保存房间流水
	 */
	@RequiresPermissions("web:tRoomBill:edit")
	@Log(title = "房间流水", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(TRoomBill tRoomBill)
	{		
		
		return toAjax(tRoomBillService.updateTRoomBill(tRoomBill));
	}
	
	/**
	 * 修改保存房间流水
	 */
	@RequiresPermissions("web:tRoomBill:edit")
	@Log(title = "房间流水", businessType = BusinessType.UPDATE)
	@PostMapping("/edit4")
	@ResponseBody
	public AjaxResult editSave4(TRoomBill tRoomBill)
	{		
		TRoominfo tRoominfo = new TRoominfo();
		tRoominfo.setFjh(tRoomBill.getFjh());
		List<TRoominfo> tRoominfoList = tRoominfoService.selectTRoominfoList(tRoominfo);
		tRoominfo = tRoominfoList.get(0);
		tRoominfo.setZt("空闲");
		tRoominfo.setKssj(null);
		
		tRoominfoService.updateTRoominfo(tRoominfo);
		
		
		return toAjax(tRoomBillService.updateTRoomBill(tRoomBill));
	}
	
	
	/**
	 * 修改保存房间流水
	 */
	@RequiresPermissions("web:tRoomBill:edit")
	@Log(title = "房间流水", businessType = BusinessType.UPDATE)
	@PostMapping("/edit3")
	@ResponseBody
	public AjaxResult editSave3(TRoomBill tRoomBill)
	{		
		return toAjax(tRoomBillService.updateTRoomBill(tRoomBill));
	}

	/**
	 * 删除房间流水
	 */
	@RequiresPermissions("web:tRoomBill:remove")
	@Log(title = "房间流水", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(tRoomBillService.deleteTRoomBillByIds(ids));
	}

}
